import Vue from 'vue'
import Vuex from 'vuex'
import common from './modules/common'

Vue.use(Vuex)

const type={
  SET_USER:"SET_USER",
  MENU:"MENU",
  DYNAMICMENUROUTES:"DYNAMICMENUROUTES"
}
export default new Vuex.Store({

  modules:{
    common
  },
  state:{
    user:{},
    menuList:[],
    dynamicMenuRoutes:[]
  },
  getters:{
    user:state => state.user,
    menuList:state=>state.menuList,
    dynamicMenuRoutes:state=>state.dynamicMenuRoutes
  },
  mutations:{
    [type.SET_USER](state,user){
      if (user) state.user=user;
      else state.user={};
    },
    [type.MENU](state,menuList){
      if(menuList) state.menuList=menuList;
      else state.menuList=[];
    },
    [type.DYNAMICMENUROUTES](state,dynamicMenuRoutes){
      if(dynamicMenuRoutes) state.dynamicMenuRoutes=dynamicMenuRoutes;
      else state.dynamicMenuRoutes=[];
    }
  },
  actions:{
    setUser:({commit},user)=>{
      commit(type.SET_USER,user)
    },
    setMenu:({commit},menuList)=>{
      commit(type.MENU,menuList)
    },
    setDynamicRoute:({commit},dynamicMenuRoutes)=>{
      commit(type.DYNAMICMENUROUTES,dynamicMenuRoutes)
    }
  },
  type
})
